package com.hk.service.impl;

import com.hk.dao.DisplayDao;
import com.hk.model.Display;
import com.hk.model.Document;
import com.hk.model.ProductMenu;
import com.hk.service.IDisplayService;
import com.hk.service.IProductDisplayService;
import com.hk.service.IProductMenuService;
import com.hk.utils.Io;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/19 9:42
 * State：产品展示业务逻辑层
 */
public class DisplayServiceImpl implements IDisplayService {

    List<Display> displays = null;

    /**
     * 产品展示
     * @param path
     * @return
     */
    @Override
    public List<Display> findAllDisplay(String path) {
        DisplayDao displayDao = new DisplayDao();
//        获取所有产品展示
        displays = displayDao.findAllDisplay();
//      1、存储路径  2、文件夹 3、文件
        Document document = new Document(path,"Display", "Display.txt");
//        检索文件路径和文件夹或文件并写入数据
        writeData(displays,document);
//        读取数据
        displays = (List<Display>) readData(document);
//        获取产品父子级菜单
        IProductDisplayService iProductDisplayService = new ProductDisplayServiceImpl();
        //    产品菜单Service
        IProductMenuService iProductMenuService = new ProductMenuServiceImpl();
        List<ProductMenu> productMenus = iProductMenuService.findByParentId(path, 0);
        if (displays == null && displays.isEmpty()) {
            System.out.println("产品展示没有数据");
            return null;
        }
        //            遍历父菜单
        for (ProductMenu productMenu : productMenus) {
            for (Display display : displays) {
//                    判断产品属于哪个一级菜单
                if (display.getMenuId() == productMenu.getMenuId()) {
//                        封装一级菜单
                    ProductMenu productMenu1 = new ProductMenu(
                            productMenu.getMenuId(),
                            productMenu.getMenuName(),
                            productMenu.getMenuUrl(),
                            productMenu.getParentId()
                    );
                    display.setProductMenu1(productMenu1);
//            遍历子菜单
                    for (ProductMenu menu : productMenu.getProductMenus()) {
//                            判断产品属于哪个子菜单
                        if (display.gettMenuId() == menu.getMenuId()) {
                            display.setProductMenu(menu);
                        }
                    }
                }
            }
        }
        return displays;

    }

    /**
     * 读取数据
     * @param document
     * @return
     */
    @Override
    public List<?> readData(Document document) {
        //    根据该对象内多少属性进行转化划分
        final int count = 8;
        displays = new ArrayList<>();
        List<String> list = Io.readData(document);
        for (int i = 0; i < list.size(); i = i+count) {
            BigDecimal marketPrice = new BigDecimal(list.get(i+4));
            BigDecimal platformPrice = new BigDecimal(list.get(i+5));
            Display display = new Display(
                    Integer.parseInt(list.get(i)),
                    list.get(i+1),
                    list.get(i+2),
                    list.get(i+3),
                    marketPrice,
                    platformPrice,
                    Integer.parseInt(list.get(i+6)),
                    Integer.parseInt(list.get(i+7))
            );
            displays.add(display);
        }
        return displays;
    }

    /**
     * 写入数据
     * @param list
     * @param document
     */
    @Override
    public void writeData(List<?> list, Document document) {
        displays = (List<Display>) list;
        List<String> list1 = new ArrayList<>();
        for (Display display : displays) {
            list1.add(String.valueOf(display.getProductId()));
            list1.add(display.getProductImg());
            list1.add(display.getProductName());
            list1.add(display.getProductDtl());
            list1.add(String.valueOf(display.getMarketPrice()));
            list1.add(String.valueOf(display.getPlatformPrice()));
            list1.add(String.valueOf(display.gettMenuId()));
            list1.add(String.valueOf(display.getMenuId()));
        }
        Io.writeData(list1,document);

    }
}
